package dao

import (
	"booksPro/model"
	"booksPro/utils"
	"fmt"
	"strconv"
)

// 获取所有图书数据
func GetBooks() ([]*model.Book, error) {
	sqlStr := "select id,title,author,price,sales,stock,img_path from books"
	rows, err := utils.Db.Query(sqlStr)
	if err != nil {
		return nil, err
	}
	var books []*model.Book
	for rows.Next() {
		book := &model.Book{}
		//将查到的数据 赋值到book的属性上
		rows.Scan(&book.ID, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock, &book.ImgPath)
		//将每个新的book放入books切片中
		books = append(books, book)
	}
	return books, nil
}

// 向数据库添加图书
func AddBook(b *model.Book) error {
	sqlStr := "insert into books(title,author,price,sales,stock,img_path) values(?,?,?,?,?,?)"
	_, err := utils.Db.Exec(sqlStr, b.Title, b.Author, b.Price, b.Sales, b.Stock, b.ImgPath)
	if err != nil {
		return err
	}
	return nil
}

// 根据id删除book
func DeleteBook(bookId string) error {
	fmt.Println("删除图书")
	sqlStr := "delete from books where id = ?"
	_, err := utils.Db.Exec(sqlStr, bookId)
	if err != nil {
		return err
	}
	fmt.Println("删除成功")
	return nil
}

// 根据bookid查询出一本图书
func GetBookById(bookId string) (*model.Book, error) {
	sqlStr := "select id,title,author,price,sales,stock,img_path from books where id = ?"
	row := utils.Db.QueryRow(sqlStr, bookId)
	book := &model.Book{}
	row.Scan(&book.ID, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock, &book.ImgPath)
	return book, nil
}

// 更新book
func UpdateBook(b *model.Book) error {
	sqlStr := "update books set title=?,author=?,price=?,sales=?,stock=? where id = ? "
	_, err := utils.Db.Exec(sqlStr, b.Title, b.Author, b.Price, b.Sales, b.Stock, strconv.FormatInt(int64(b.ID), 10))
	if err != nil {
		return err
	}
	fmt.Println("更新完成")
	return nil
}

// 获取 带分页的图书信息 ,需要带上页数查询
func GetPageBooks(pageNo string) (*model.Page, error) {
	//把 传入的stirng类型的 页码 转为int64
	iPageNo, _ := strconv.ParseInt(pageNo, 10, 64)
	// fmt.Println("查询记录数,将数据赋值给totalRecord")
	sqlStr := " select COUNT(*) from books"
	var totalRecord int64
	row := utils.Db.QueryRow(sqlStr)
	row.Scan(&totalRecord)
	// fmt.Println("总记录数", totalRecord)
	// fmt.Println("设置每页显示4条")
	var pageSize int64 = 4
	// fmt.Println("如果记录数/每页4条=整数 得到 总页数  如果记录数/每页4条!=整数 就+1")
	var totalPageNo int64
	if totalRecord%pageSize == 0 {
		totalPageNo = totalRecord / pageSize
	} else {
		totalPageNo = totalRecord/pageSize + 1
	}
	// fmt.Println("获取当前页中的图书")
	sqlStr2 := "select id,title,author,price,sales,stock,img_path from books limit ?,?"
	rows, err := utils.Db.Query(sqlStr2, (iPageNo-1)*pageSize, pageSize)
	if err != nil {
		return nil, err
	}
	// fmt.Println("将获取到的图书放入切片中")
	var books []*model.Book
	for rows.Next() {
		book := &model.Book{}
		rows.Scan(&book.ID, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock, &book.ImgPath)
		//将book放入到切片
		books = append(books, book)
	}
	// fmt.Println("给page对象赋值,并返回")
	page := &model.Page{
		Books:       books,
		PageNo:      iPageNo,
		PageSize:    pageSize,
		TotalPageNo: totalPageNo,
		TotalRecord: totalRecord,
	}
	return page, nil
}

func GetPageBooksByPrice(minPrice string, maxPrice string, pageNo string) (*model.Page, error) {
	// fmt.Println("设置每页显示4条")
	var pageSize int64 = 4
	sqlStr := "select  id,title,author,price,sales,stock,img_path from books where price >=? and price <=? limit ?,?"
	rows, _ := utils.Db.Query(sqlStr, minPrice, maxPrice, pageNo, pageSize)
	var books []*model.Book
	for rows.Next() {
		book := &model.Book{}
		rows.Scan(&book.ID, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock, &book.ImgPath)
		books = append(books, book)
	}
	//把 传入的stirng类型的 页码 转为int64
	iPageNo, _ := strconv.ParseInt(pageNo, 10, 64)
	// fmt.Println("查询记录数,将数据赋值给totalRecord")
	sqlStr2 := " select COUNT(*) from books where price >=? and price <=? "
	var totalRecord int64
	row := utils.Db.QueryRow(sqlStr2, minPrice, maxPrice)
	row.Scan(&totalRecord)
	// fmt.Println("总记录数", totalRecord)

	// fmt.Println("如果记录数/每页4条=整数 得到 总页数  如果记录数/每页4条!=整数 就+1")
	var totalPageNo int64
	if totalRecord%pageSize == 0 {
		totalPageNo = totalRecord / pageSize
	} else {
		totalPageNo = totalRecord/pageSize + 1
	}
	page := &model.Page{
		Books:       books,
		PageNo:      iPageNo,
		PageSize:    pageSize,
		TotalPageNo: totalPageNo,
		TotalRecord: totalRecord,
	}
	return page, nil

}
